// 示例首页
var util = require('../../utils/util.js')
// 模拟登录页面,其他并没什么卵用
import authControl from '../../utils/authControl'
// 日签底图
const boradBgImg = require('../../utils/data.js').boradBgImg

var timestamp = +new Date()

import { common } from '../../services/index.js'
//获取应用实例
const app = getApp()

var bannerResult = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var isPreviewCreate = false

Page({
  
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      return {
        title: '我的心里话都在这里',
        path: '/pages/index/index?shareIndex=' + this.data.current,
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
      this.hideMenu()
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '我的心里话都在这里',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
 
  data: {

    months: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
    isOpen : false , 
    msg: '' ,
    
    isPass: false ,
    // app.globalData 全局变量
    prefix: app.globalData.prefix,

    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
        
    // swipe
    imgUrls: [
    ],
    indicatorDots: false,
    autoplay: false,
    circular : true ,
    interval: 5000,
    duration: 1000,
    current: 0,
    isScroll : false , // 用户触摸时用于隐藏 两侧 图片，形成视觉差

    // actionsheet 
    isShowMenu: false ,
    qrcodeUrl: '', // 合成图底部 二维码 下载到本地 

    isShowLoading: true ,
    // 默认生成第一张和第二张
    flag: -1 ,// 根据背景图索引触发 生成新的日签的  标志
    qrCodeUrl : '' // 二维码链接
  },
  onReady(){
    
  },
  onHide(){
  },
  onUnload(){
    // 点击右上角收起时触发
    // 把已生成的日签图片链接删除
    bannerResult = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  },
  onLoad: function (options) {
    var daySignData = wx.getStorageSync('daySign')
    this.setData({
      headImgUrl: daySignData.headImgUrl,
      userName: daySignData.userName,
      createTime: Number(daySignData.createTime),
      activityName: daySignData.activityName,
      activityId: daySignData.activityId,
      insistDay: daySignData.insistDay,
      creator: daySignData.creator ,
      momentId: daySignData.momentId,
      
    })
    var date = new Date(this.data.createTime)
    var year = date.getFullYear().toString()
    var month = date.getMonth().toString()
    var day = date.getDate()
    var yearStr = year
    var monthStr = this.data.months[month] // 中文
    day = day < 10 ? '0' + day : day

    this.setData({
      year ,
      monthStr ,
      day
    })


    console.log('检测登陆')
    var that = this
    authControl.login(app).then((res) => {
      this.setData({
        imgUrls: boradBgImg
      })
      console.log('已调用登陆')
      var that = this
      var dateObj = new Date(this.data.createTime)
      var month = dateObj.getMonth() + 1 < 10 ? '0' + (dateObj.getMonth() + 1) : (dateObj.getMonth() + 1)
      var dateStr = dateObj.getFullYear() + '-' + month + '-' + (dateObj.getDate() < 10 ? '0' + dateObj.getDate() : dateObj.getDate())
        var scene = 'id=' + this.data.activityId + '&mId=' + this.data.momentId + '&d=' + dateStr
        console.log(scene)
        console.log('id=' + this.data.activityId + '&momentId=' + this.data.momentId + '&date=' + dateStr)
        common.createMiniQRCode({
          //appId: config.appid ,
          appId: daySignData.skipAppId,
          pages: 'pages/attendancePreview/attendancePreview',
          scene: scene,
          width: 174,
        }).then(res => {
          console.log('获取二维码成功', res)
          this.setData({
            qrCodeUrl: app.globalData.prefix + res.data.qrCodeUrl,
          })
          wx.setStorageSync('qrcodeUrl', app.globalData.prefix + res.data.qrCodeUrl)
            
        })  

      
      wx.getStorageInfo({
        success: function (res) {
          console.log('小程序缓存', res)
        },
      },this)
      if (options.shareIndex) {
        this.setData({
          current: options.shareIndex
        })
      }
    }, (err) => {
      console.log('调用登陆是被')
      console.log('登录失败', err);
      wx.showModal({
        content: '登录失败了,看看后台返回什么',
        showCancel: false
      })
    })  
  },
  skipToTmpList() {
    wx.navigateTo({
      url: '/pages/tmpList/index'
    })
  },
  // swiper 
  swiperChange(e) {
    this.setData({
      current: e.detail.current
    })
    // this.setData({
    //   current: e.detail.current,
    //   flag : e.detail.current  ,
      
    // },()=>{
    //   // var change = ++e.detail.current
    //   // if(change > 8){
    //   //   return 
    //   // } 
    //   // this.setData({
    //   //   flag: e.detail.current,
    //   // })
    // })
  },
  swiperTouchStart() {
    console.log('start')
    this.setData({
      isScroll: true,
    })
  },
  swiperTouchEnd() {
    console.log('end')
    var that = this
    setTimeout(function () {
      that.setData({
        isScroll: false,
      })
    }, 400)
  },
  beforeAsAtAlumb(){
    var currentImage = bannerResult[this.data.current]
    if (currentImage){
      this.saveAsAtAlumb(currentImage)
    }
    else{
      this.setData({
        flag: this.data.current
      })
    }
    
  },
  previewBannerImage(){
     
    var currentImage = bannerResult[this.data.current]
    if (currentImage){
        this.preview(currentImage)
    }
    else{
      isPreviewCreate = true 
      this.setData({
        flag: this.data.current
      })
    }
  },
  preview(path){
    wx.previewImage({
      urls: [path],
    })
  },
  // 保存在相册
  saveAsAtAlumb( path ) {
    var that = this
    wx.showToast({
      title: '正在保存...',
      icon: 'loading',
      duration: 1000
    })
    wx.saveImageToPhotosAlbum({
      filePath : path ,
      success: function () {
        wx.showModal({
          title: '小提示',
          content: '日签已保存到你的相册 \r\n 快分享到朋友圈！',
          // content: '快分享给小伙伴们一起围观吧～',
          showCancel: false,
        })
      },
      fail: function () {
        wx.showModal({
          title: '小提示',
          content: '快分享给小伙伴们一起围观吧～',
          showCancel: false,
        })
      }
    })
    // wx.downloadFile({
    //   url: demoImgUrls[this.data.current],
    //   success: function (res) {
    //     wx.hideToast()
    //     that.hideMenu()
    //     console.log('download success');
    //     console.log(res.tempFilePath)
    //     //that.createNewImg(res.tempFilePath)
    //   }
    // })

  },
  previewImage(){
    
  },
  successCreate(obj){
    console.log(obj.detail)
    // imgUrls[obj.detail.index] = obj.detail.tempFilePath
    // this.setData({
    //   imgUrls
    // })
    if (isPreviewCreate){
      isPreviewCreate = false
      this.preview(obj.detail.tempFilePath)
    }
    else{
      this.saveAsAtAlumb(obj.detail.tempFilePath)
    }
    
    bannerResult[this.data.current] = obj.detail.tempFilePath

  }

})
